﻿Imports Telerik.Windows.Controls
Imports Telerik.Windows.Data
Imports CRM.BusinessLayer
Imports System.Data
Imports System.Text
Partial Public Class frmAdminIndustries
    Private intSaveNeworEdit As Integer
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmAdminTitles_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        refreshList()
        Me.txtIndustry.IsEnabled = False
        Me.btnSave.IsEnabled = False
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = False
        Me.btnDelete.IsEnabled = False
    End Sub
    Private Sub refreshList()
        Me.grdIndustries.ItemsSource = Admin.IndustryList
        Me.grdIndustries.Columns(0).IsVisible = False
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddNew.Click
        intSaveNeworEdit = 1
        ClearControls()
        Me.txtIndustry.IsEnabled = True
        Me.btnAddNew.IsEnabled = False
        Me.btnSave.IsEnabled = True
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = True
        Me.btnDelete.IsEnabled = False
        Me.txtIndustry.Focus()
    End Sub

    Private Sub ClearControls()
        Me.txtIndustry.Text = String.Empty
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        intSaveNeworEdit = 0
        ClearControls()
        Me.txtIndustry.IsEnabled = False
        Me.btnAddNew.IsEnabled = True
        Me.btnSave.IsEnabled = False
        Me.btnEdit.IsEnabled = False
        Me.btnCancel.IsEnabled = False
        Me.btnDelete.IsEnabled = False
        Me.grdIndustries.IsEnabled = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEdit.Click
        intSaveNeworEdit = 2
        ClearControls()
        Me.txtIndustry.IsEnabled = True
        Me.btnSave.IsEnabled = True
        Me.btnCancel.IsEnabled = True
        Me.btnDelete.IsEnabled = False
    End Sub

    Private Sub grdIndustries_SelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.SelectionChangeEventArgs) Handles grdIndustries.SelectionChanged

        If Me.grdIndustries.SelectedItems.Count = 0 Then
            Me.grdIndustries.IsEnabled = False
            Me.btnEdit.IsEnabled = False
            Me.btnCancel.IsEnabled = False
            Me.btnDelete.IsEnabled = False
            Exit Sub
        Else
            'Me.grdIndustries.IsEnabled = False
            Me.btnEdit.IsEnabled = True
            Me.btnCancel.IsEnabled = True
            Me.btnDelete.IsEnabled = True
            Dim selectedRow As DataRow = grdIndustries.SelectedItem
            If Not IsNothing(selectedRow) Then
                txtIndustry.Text = selectedRow(1).ToString()
            End If
        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        If txtIndustry.Text.Trim = String.Empty Then
            MsgBox("Please enter Industry !")
            txtIndustry.Focus()
            Exit Sub
        Else
            btnSave.IsEnabled = False

            'grdConsult.Enabled = True
            Dim sql As StringBuilder
            Select Case intSaveNeworEdit
                Case 1 'new one
                    sql = New StringBuilder
                    With sql
                        .Append("")
                        .Append("insert into l_industry ")
                        .Append("(Industry_Name)")
                        .Append(" Values(")
                        .Append(" '" & Me.txtIndustry.Text & "'")
                        .Append(")")
                    End With

                    If CommonDataFunctions.ExecuteSQL(sql.ToString) = True Then
                        MsgBox("The new Industry has been added successfully.", MsgBoxStyle.Information, "Success")
                        'Exit Sub
                    Else
                        MsgBox("There is an error adding the Industry to the system. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                        'Exit Sub
                    End If
                Case 2 'existing one
                    sql = New StringBuilder
                    With sql
                        .Append("update l_industry set ")
                        .Append(" Industry_Name = '" & Me.txtIndustry.Text & "'")
                        Dim selectedRow As DataRow = grdIndustries.SelectedItem
                        .Append(" where Industry_ID = " & selectedRow(0))
                    End With

                    If CommonDataFunctions.ExecuteSQL(sql.ToString) = True Then
                        MsgBox("The Industry's details have been updated successfully.", MsgBoxStyle.Information, "Success")
                        'Exit Sub
                    Else
                        MsgBox("There is an error in editing the Industry's details. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                        'Exit Sub
                    End If

            End Select
            refreshList()
            ClearControls()
            txtIndustry.IsEnabled = False
            grdIndustries.IsEnabled = True
            btnCancel.IsEnabled = False
            btnAddNew.IsEnabled = True
            btnEdit.IsEnabled = False
            btnCancel.IsEnabled = False
        End If

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click
        Dim deleteOption As MsgBoxResult
        deleteOption = MsgBox("Are you really sure to delete Industry ?", MsgBoxStyle.OkCancel)
        If deleteOption = MsgBoxResult.Ok Then
            Dim selectedRow As DataRow = grdIndustries.SelectedItem
            Dim Sql As New StringBuilder
            With Sql
                .Append("delete  from l_industry where Industry_ID=" + selectedRow(0).ToString())
            End With

            If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                btnDelete.IsEnabled = False
                btnCancel.IsEnabled = False
                btnEdit.IsEnabled = False
                btnSave.IsEnabled = False
                grdIndustries.IsEnabled = True
                ClearControls()
                refreshList()
                MsgBox("The Industry Details has been deleted successfully.", MsgBoxStyle.Information, "Success")
            Else
                MsgBox("There is an error while deleting the Industry to the system. Please try again or contact IT", MsgBoxStyle.Critical, "Unsuccessful")
                'Exit Sub
            End If

        ElseIf deleteOption = MsgBoxResult.Cancel Then

        End If

    End Sub
End Class
